// eventBus.ts
export default class EventBus {
  private static instance: EventBus | null = null;
  private events: Record<string, Function[]> = {};

  private constructor() {}

  public static getInstance(): EventBus {
    if (!EventBus.instance) {
      EventBus.instance = new EventBus();
    }
    return EventBus.instance;
  }

  // 注册事件监听器
  on(eventName: string, callback: Function) {
    this.events[eventName] = this.events[eventName] || [];
    this.events[eventName].push(callback);
  }

  // 触发事件
  emit(eventName: string, ...args: any[]) {
    (this.events[eventName] || []).forEach(callback => callback(...args));
  }

  // 移除事件监听器
  off(eventName: string, callback?: Function) {
    if (callback) {
      this.events[eventName] = this.events[eventName]?.filter(cb => cb !== callback);
    } else {
      delete this.events[eventName];
    }
  }
}